Python请求包:处理xml响应

您所在的位置:网站首页 xmltodict 安装 Python请求包:处理xml响应

Python请求包:处理xml响应

2022-11-06 05:07| 来源: 网络整理| 查看: 265

requests不处理解析XML响应,不。XML响应在本质上比JSON响应复杂得多,如何将XML数据序列化为Python结构并不那么简单。 Python自带XML解析器。我建议您使用ElementTree API:

import requests from xml.etree import ElementTree response = requests.get(url) tree = ElementTree.fromstring(response.content)

或者,如果响应特别大,则使用增量方法:

response = requests.get(url, stream=True) # if the server sent a Gzip or Deflate compressed response, decompress # as we read the raw stream: response.raw.decode_content = True events = ElementTree.iterparse(response.raw) for event, elem in events: # do something with `elem`

外部lxml项目构建在相同的API上,为您提供更多功能和强大功能。



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3